﻿using System;
using System.Collections.Generic;
using com.yoozoo.gta.Gameplay.PVE;
using Gameplay.PVE;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.City
{
    public class CityRenderSettingMgr : MonoBehaviour
    {
        
        [SerializeField]
        private List<Texture2D> lightMapColors;
        [SerializeField]
        private List<Texture2D> lightMapDirs;
        [SerializeField]
        private GameObject skyBoxObject;
        [SerializeField]
        private Material skyBoxMaterial;
        [SerializeField]
        private GameObject reflectionProbe;

        [SerializeField]
        private SceneRenderData sceneRenderData;
        
        private ResLoader lightMapLoader;
        
        // 应用渲染设置
        public void ApplyRenderSetting(GgameSceneConfig mapData,Action action)
        {
            sceneRenderData = mapData.RenderData;
            lightMapLoader = ResLoader.Alloc();
            lightMapColors = new List<Texture2D>();
            lightMapDirs = new List<Texture2D>();

            for (int i = 0; i < sceneRenderData.lightMapColorPath.Count; i++)
            {
                int index = i;
                lightMapColors.Add(null);
                lightMapLoader.Add2Load(sceneRenderData.lightMapColorPath[i], (s, an, a) =>
                {
                    if (s)
                    {
                        lightMapColors[index] = a as Texture2D;
                    }
                });
            }
            
            for (int i = 0; i < sceneRenderData.lightMapDirPath.Count; i++)
            {
                int index = i;
                lightMapDirs.Add(null);
                lightMapLoader.Add2Load(sceneRenderData.lightMapDirPath[i], (s, an, a) =>
                {
                    if (s)
                    {
                        lightMapDirs[index] = a as Texture2D;
                    }
                });
            }
                    
            lightMapLoader.Add2Load(sceneRenderData.skyBoxMaterialPath, (s, an, a) =>
            {
                if (s && a != null && a is Material go)
                {
                    skyBoxMaterial = Material.Instantiate(go);
                }

            });
            
            
            if (!string.IsNullOrEmpty(sceneRenderData.reflectionProbePath))
            {
                lightMapLoader.Add2Load(sceneRenderData.reflectionProbePath, typeof(GameObject), (success, assetName, asset) =>
                {
                    if (success)
                    {
                        reflectionProbe = GameObject.Instantiate(asset as GameObject);
                    }
                });
            }

            
            
            lightMapLoader.Load(() =>
            {
                ApplySetting();
                action?.Invoke();
            });
        }


        public void ApplySetting()
        {
            RenderSettings.ambientMode = sceneRenderData.ambientMode;

            var sceneLightMap = new LightmapData[lightMapColors.Count];
            for (int i = 0; i < sceneLightMap.Length; i++)
            {
                sceneLightMap[i] = new LightmapData
                {
                    lightmapColor = lightMapColors[i],
                    lightmapDir = lightMapDirs[i],
                };
            }

            LightmapSettings.lightmaps = sceneLightMap;
            LightmapSettings.lightmapsMode = LightmapsMode.CombinedDirectional;

            RenderSettings.ambientLight = sceneRenderData.ambientColor;
            RenderSettings.defaultReflectionMode = sceneRenderData.reflectionMode;
            RenderSettings.skybox = skyBoxMaterial;
            RenderSettings.ambientSkyColor = sceneRenderData.ambientColor;

            RenderSettings.fog = sceneRenderData.isFog;
            RenderSettings.fogColor = sceneRenderData.fogColor;
            RenderSettings.fogMode = sceneRenderData.fogMode;
            RenderSettings.fogStartDistance = sceneRenderData.fogStart;
            RenderSettings.fogEndDistance = sceneRenderData.fogEnd + 250;
            
            if (reflectionProbe)
            {
                reflectionProbe.transform.position = Vector3.zero;
                reflectionProbe.transform.rotation = Quaternion.identity;
            }
        }
        
        
        public void OnDispose()
        {
            if (lightMapLoader!=null)
            {
                lightMapLoader.Recycle2Cache();
                lightMapLoader = null;
            }

            if (reflectionProbe)
            {
                GameObject.DestroyImmediate(reflectionProbe);
            }
        }
    }
}
